<?php

//System.class.php
//静态的系统类，核心，全局总调


namespace Arox\System;

final class System{
	
	private static $contro;
	private static $action;
	private static $param;

	//防止System::run被重复调用
	private static $numRun = FALSE;
	
	public function __construct(){
		require AROX_FUNC.'Common.func.php';//载入公共函数文件
		require AROX_SYSTEM.'Loader.class.php';//加载载入类文件
	}
	
	//初始化
	private static function initialize(){
		
		Loader::registerAutoload();//自动加载
		Config::init();
		self::controller();
	}
	
	//路由处理
	private static function route(){
		if(Config::get('is_route')){
			//调用路由类
			
		}else{
			//不启用路由，就是普通GET请求了
			$contro = Config::get('controllerC');
			$action = Config::get('controllerA');
			//strtolower — 将字符串转化为小写
			//strtoupper — 将字符串转化为大写
			$strtolowerContro = strtolower($contro);
			$strtoupperContro = strtoupper($contro);
			$strtolowerAction = strtolower($action);
			$strtoupperAction = strtoupper($action);
			//控制器
			if(isset($_GET[$strtolowerContro]) && !empty($_GET[$strtolowerContro])){
				self::$contro = $_GET[$strtolowerContro];
			}elseif(isset($_GET[$strtoupperContro]) && !empty($_GET[$strtoupperContro])){
				self::$contro = $_GET[$strtoupperContro];
			}else{
				self::$contro = Config::get('controllerDefaultC');
			}
			
			//动作方法
			if(isset($_GET[$strtolowerAction]) && !empty($_GET[$strtolowerAction])){
				self::$action = $_GET[$strtolowerAction];
			}elseif(isset($_GET[$strtoupperAction]) && !empty($_GET[$strtoupperAction])){
				self::$action = $_GET[$strtoupperAction];
			}else{
				self::$action = Config::get('controllerDefaultA');
			}
			
			unset($_GET[$strtolowerContro],$_GET[$strtoupperContro],$_GET[$strtolowerAction],$_GET[$strtoupperAction]);
			
			self::$param = $_GET;
			
			unset($_GET);
		}
	}
	
	//控制器处理
	private static function controller(){
		self::route();
		//stat
		$controllerName = self::$contro.'Controller';
		$actionName = self::$action.'Action';
		$controllerFile = APP_PATH.APP_NAME.DS.'controller'.DS.$controllerName.'.class.php';
		if(file_exists($controllerFile)){
			Loader::load($controllerFile);
		}
	}
	//过滤处理
	//钩子处理
	//运行
	public function run(){
		if(self::$numRun){
			return;
		}else{
			self::$numRun = TRUE;
		}
		self::initialize();

	}
}